package com.li.controller;


import com.common.util.MyFileUtil;

import com.li.model.MyMassage;
import com.li.model.TableData;
import com.li.model.User;
import com.li.model.UserFile;

import com.li.service.UserFileServiceImpl;
import com.li.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.util.Collections;
import java.util.List;
@Api(tags = "用户文件管理")
@RestController
public class UserFileServlet {
    @Autowired
    private UserService userService;
    @Autowired
    private UserFileServiceImpl userFileService;
    @ApiOperation("通过文件id删除用户文件")
    @GetMapping("/com/userFile/deleteFile/{id}")
    public ResponseEntity<MyMassage> deleteFile(@PathVariable long id){
        UserFile userFile = userFileService.queryFileById(id);
        String targetFile = userFile.getSavePath()+'/'+userFile.getFileName();
        File file = new File(targetFile);
        if (file.delete()){
            System.out.println("删除文件："+targetFile);
        }else {
            System.out.println("删除文件失败："+targetFile);
            return ResponseEntity.ok(new MyMassage("删除失败",null,400));
        }
        userFileService.deleteFileById(id);
        return ResponseEntity.ok(new MyMassage("删除成功",null,200));
    }

    @ApiOperation("通过用户id查询用户的所有文件")
    @GetMapping("/admin/userFile/queryUserFile/{id}")
    public ResponseEntity<MyMassage> queryFileById(@PathVariable long id){
        List<UserFile> files = userFileService.queryFileByUserId(id);
        if (CollectionUtils.isEmpty(files)){
            return ResponseEntity.ok(new MyMassage("此用户没有文件",null,200));
        }
        return ResponseEntity.ok(new MyMassage("得到用户文件",files,200));
    }
    @ApiOperation("查询当前用户的所有文件")
    @GetMapping("/com/userFile/queryUserFile")
    public ResponseEntity<MyMassage>  queryFileById(){
        Subject currentSubject = SecurityUtils.getSubject();
        Session session = currentSubject.getSession();
        User user = (User)session.getAttribute("USER_SESSION");
        if (user==null){
            return ResponseEntity.ok(new MyMassage("请先登录",Collections.emptyList(),400));
        }
        List<UserFile> files = userFileService.queryFileByUserId(user.getId());
        if (CollectionUtils.isEmpty(files)){
            return ResponseEntity.ok(new MyMassage("当前用户为上传文件",Collections.emptyList(),200));
        }
        return ResponseEntity.ok(new MyMassage("得到当前用户文件",files,200));
    }
    @ApiOperation("用户文件上传")
    @PostMapping("/com/userFile/upload")
    public ResponseEntity<MyMassage> upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) {
        System.out.println(file.getName());
        String msg = userFileService.upload(file,request);
        return ResponseEntity.ok(new MyMassage(msg,null,200));
    }
    @ApiOperation("通过文件id下载文件")
    @GetMapping("/download/{id}")
    public ResponseEntity<MyMassage> download( HttpServletResponse response, @PathVariable long id){
        UserFile userFile = userFileService.queryFileById(id);
        if (MyFileUtil.fileDownload(response,userFile.getFileName(),userFile.getSavePath())){
            return ResponseEntity.ok(new MyMassage("下载成功",null,200));
        }
        return ResponseEntity.ok(new MyMassage("下载失败",null,200));
    }
    @ApiOperation("通过文件id得到文件内容")
    @GetMapping("/fileShow/{fid}")
    public ResponseEntity<TableData> fileShow(@ApiParam("文件名") @PathVariable("fid")long fid, HttpServletRequest request){
        UserFile userFile = userFileService.queryFileById(fid);
        if (userFile == null){
            return ResponseEntity.notFound().build();
        }
         return ResponseEntity.ok(userFileService.fileToList(userFile.getFileName(),fid,request));
    }
    @ApiOperation("查询所有用户文件")
    @GetMapping("/admin/userFile/queryAll")
    public ResponseEntity<MyMassage> queryAll(){
        List<UserFile> files = userFileService.queryAllFile();
        if (CollectionUtils.isEmpty(files)){
            return ResponseEntity.ok(new MyMassage("目前无文件", Collections.emptyList(),200));
        }
        return ResponseEntity.ok(new MyMassage("获取所有用户文件",files,200));
    }

}
